/**
 *
 */
package wsepForum.ui.swing.outerPanels;

import java.awt.Font;
import java.awt.event.*;
import java.util.concurrent.ExecutionException;
import javax.swing.*;
import java.util.Vector;
import wsepForum.network.client.GUIController;
import wsepForum.network.common.GUIAnswer;
import wsepForum.network.common.GUIAnswer.AnswerType;
import wsepForum.network.common.GUIRequest.RequestType;
import wsepForum.ui.swing.SetSwitcher;
import wsepForum.ui.swing.SetSwitcher.SetType;
import wsepForum.ui.swing.innerPanels.*;
import wsepForum.ui.swing.workers.ForumWorker;

public class AOuterPanel extends JPanel {
	/**
	 *
	 */
	private static final long serialVersionUID = 4366930131084905070L;
	/**
	 *
	 */
	protected AInnerPanel innerPanel;
	/**
	 *
	 */
	protected GUIController connection;
	/**
	 *
	 */
	protected SetSwitcher switcher;
	
	private JLabel numOfConnectedLabel;

	public AOuterPanel(GUIController connection, final SetSwitcher switcher) {
		this.connection = connection;
		this.switcher = switcher;
		removeAll();
		this.innerPanel = null;
		setLayout(null);
		JLabel lblWsepForum = new JLabel("WSEP FORUM");
		lblWsepForum.setFont(new Font("Cooper Black", Font.PLAIN, 18));
		lblWsepForum.setHorizontalAlignment(SwingConstants.CENTER);
		lblWsepForum.setBounds(278, 0, 147, 40);

		JLabel lblStatus = new JLabel("["+getCurrentSet().name()+"]");
		lblStatus.setFont(new Font("Cooper Black", Font.PLAIN, 16));
		lblStatus.setHorizontalAlignment(SwingConstants.CENTER);
		lblStatus.setBounds(-3, 453, 147, 40);

		add(lblWsepForum);
		add(lblStatus);
		addNumberOfConnectedLabel();
		addHomeButton();
		addSearchButton();
		setBounds(0, 0, 761, 527);
		
	}

	protected JButton buttonFactory(String name, int coor1, int coor2, int coor3, int coor4) {
		final JButton button = new JButton(name);
		button.setFont(new Font("Tahoma", Font.PLAIN, 11));
		button.setBounds(coor1, coor2, coor3, coor4);
		add(button);
		return button;
	}

	public GUIController getConnection(){
		return this.connection;
	}

	protected void addHomeButton() {
		JButton homeButton = this.buttonFactory("Home", 330, 42, 97, 40);
		homeButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {

				ForumWorker<AInnerPanel> worker = new ForumWorker<AInnerPanel>(connection){

					@Override
					protected AInnerPanel doInBackground() throws Exception {
						AInnerPanel returnedPanel = requestHomePanel(AOuterPanel.this);
						return returnedPanel;
					}

					@Override
					public void done(){
						try {
							swapInnerPanel(get());
						} catch (InterruptedException e) {
							e.printStackTrace();
						} catch (ExecutionException e) {
							e.printStackTrace();
						}
					}
				};
				worker.execute();
			}
		});
		homeButton.doClick();
	}

	protected void addSearchButton() {
		JButton searchButton = this.buttonFactory("Search", 20, 42, 97, 40);
		searchButton.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {

				ForumWorker<AInnerPanel> worker = new ForumWorker<AInnerPanel>(connection){

					@SuppressWarnings("unchecked")
					@Override
					protected AInnerPanel doInBackground() throws Exception {
						GUIAnswer ans = request(RequestType.GET_USER_LIST, new Object[] {});
						setNumOfConnected(ans.getNumOfConnected());
						AInnerPanel returnedPanel = null;
						if(ans.getAnswerType().equals(AnswerType.ANSWER)){
							returnedPanel = new SearchPanel(AOuterPanel.this,(Vector<String>)ans.getArguments()[0]);
						}
						else {
							System.err.println(ans.getArguments()[0]);
						}
						return returnedPanel;
					}
					
					
					@Override
					public void done(){
						try {
							swapInnerPanel(get());
						} catch (InterruptedException e) {
						} catch (ExecutionException e) {
							e.printStackTrace();
						}
					}
				};
				worker.execute();
			}
		});
	}
	
	protected void addNumberOfConnectedLabel() {
		JLabel lblNumberOfUser = new JLabel("Number of connected users:");
		lblNumberOfUser.setFont(new Font("Cooper Black", Font.PLAIN, 16));
		lblNumberOfUser.setBounds(400, 450, 300, 39);
		add(lblNumberOfUser);
		this.numOfConnectedLabel = new JLabel("");
		this.numOfConnectedLabel.setFont(new Font("Cooper Black", Font.PLAIN, 16));
		this.numOfConnectedLabel.setBounds(665, 460, 86, 14);
		add(this.numOfConnectedLabel);
	}

	public void setNumOfConnected(int connected){
		this.numOfConnectedLabel.setText(""+connected);
		this.numOfConnectedLabel.repaint();
	}
	
	public void swapInnerPanel(AInnerPanel innerPan) {
		if (innerPan != null) {
			innerPan.setVisible(true);
			if (this.innerPanel != null)
				remove(this.innerPanel);
			add(innerPan);
			this.innerPanel = innerPan;
			this.repaint();
			this.updateUI();
		}
	}

	public SetType getCurrentSet() {
		return this.switcher.getSet();
	}

	public void changeSet(SetType type) {
		this.switcher.switchSet(type);
	}
}
